package com.hangyutech.strelitzia.vo;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hangyutech.strelitzia.constant.HttpResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

@Data
@ApiModel(value = "全局统一返回结果")
public class HttpResult {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<String, Object>();

    private HttpResult() {
    }

    public static HttpResult ok() {
        HttpResult result = new HttpResult();
        result.setSuccess(HttpResultCodeEnum.SUCCESS.getSuccess());
        result.setCode(HttpResultCodeEnum.SUCCESS.getCode());
        result.setMessage(HttpResultCodeEnum.SUCCESS.getMessage());
        return result;
    }

    public static HttpResult error() {
        HttpResult result = new HttpResult();
        result.setSuccess(HttpResultCodeEnum.UNKNOWN_REASON.getSuccess());
        result.setCode(HttpResultCodeEnum.UNKNOWN_REASON.getCode());
        result.setMessage(HttpResultCodeEnum.UNKNOWN_REASON.getMessage());
        return result;
    }


//    public static HttpResult info(String infoCode) {
//        HttpResult result = new HttpResult();
//        result.setSuccess(HttpResultCodeEnum.infoCode.getSuccess());
//        result.setCode(HttpResultCodeEnum.infoCode.getCode());
//        result.setMessage(HttpResultCodeEnum.infoCode.getMessage());
//        return result;
//    }

    public HttpResult data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }


    public HttpResult data(PageInfo map) {
        if (null == map) {
            this.setData(null);
            return this;
        }
        this.setData(HttpResult.pageInfoToMap(map));
        return this;
    }

    public HttpResult data(JSONObject map) {
        if (null == map) {
            this.setData(null);
            return this;
        }
        this.setData(map);
        return this;
    }

    public HttpResult data(Object map) {
        if (null == map) {
            this.setData(null);
            return this;
        }
        this.setData(HttpResult.objectToMap(map));
        return this;
    }


    public HttpResult data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public HttpResult message(String message) {
        this.setMessage(message);
        return this;
    }

    public HttpResult success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public HttpResult code(Integer code) {
        this.setCode(code);
        return this;
    }

    /**
     * Object 转 HashMap<String,Object>
     *
     * @param object 对象类需要有public无参构造，字段属性需要是public ，
     *               并且内部类也需要这样.
     * @return
     */
    private static HashMap pageInfoToMap(Object object) {
        return JSONObject.parseObject(JSONObject.toJSONString(object), HashMap.class);
    }

    private static Map<String, Object> objectToMap(Object obj) {
        Map<String, Object> map = new HashMap<String, Object>();
//        getDeclaredFields
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
//                if (field.getName().equals("elementData")){
//                    map.put(field.getName(), field.get(obj));
//                }
                map.put(field.getName(), field.get(obj));
            } catch (IllegalAccessException e) {
                System.out.println(e.getMessage());
//                e.printStackTrace();
            }
        }
        return map;
    }


}